﻿using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using In.Api.Dto.Notify.Notification;

namespace In.Api.Helpers
{
    /// <summary>
    /// 通知服务类
    /// </summary>
    public class NotificationService
    {

        public static string ValidateApptInfo(string apptXml, out string errMsg, out string orderId)
        {
            errMsg = "";
            var retVal = "0";
            orderId = "";
            try
            {
                var apptInfo = (ApptInfo)XmlToObject(apptXml, new ApptInfo());
                if (string.IsNullOrEmpty(apptInfo.OrderId))
                {
                    errMsg = "预约订单号为空";
                    retVal = "1";
                    return retVal;
                }
                else
                {
                    orderId = apptInfo.OrderId;
                }

                if (string.IsNullOrEmpty(apptInfo.SchduleId))
                {
                    errMsg = "班表编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.DeptId))
                {
                    errMsg = "科室编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.DoctorId))
                {
                    errMsg = "医生编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.RegTypeId))
                {
                    errMsg = "号类别编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.RegDate))
                {
                    errMsg = "预约日期为空";
                    retVal = "1";
                    return retVal;
                }
                else
                {
                    var date = new DateTime();
                    if (!DateTime.TryParse(apptInfo.RegDate, out date))
                    {
                        errMsg = "预约日期格式不正确，请参考:yyyy-MM-dd";
                        retVal = "1";
                        return retVal;
                    }
                }
                if (string.IsNullOrEmpty(apptInfo.TimeSliceId))
                {
                    errMsg = "预约时段编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.RegNo))
                {
                    errMsg = "就诊序号为空regNo";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(apptInfo.VisitNo))
                {
                    errMsg = "就诊流水号为空";
                    retVal = "1";
                    return retVal;
                }
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidateHaltInfo(string haltXml, out string errMsg)
        {
            errMsg = "";
            var retVal = "0";
            try
            {
                var haltInfo = (HaltInfo)XmlToObject(haltXml, new HaltInfo());
                if (string.IsNullOrEmpty(haltInfo.SchduleId))
                {
                    errMsg = "班表编号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(haltInfo.DeptId))
                {
                    errMsg = "科室编号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(haltInfo.DoctorId))
                {
                    errMsg = "医生编号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(haltInfo.RegDate))
                {
                    errMsg = "停诊日期为空";
                    retVal = "1";
                }
                else
                {
                    var date = new DateTime();
                    if (!DateTime.TryParse(haltInfo.RegDate, out date))
                    {
                        errMsg = "停诊日期格式不正确，请参考:yyyy-MM-dd";
                        retVal = "1";
                    }
                }


                if (string.IsNullOrEmpty(haltInfo.TimeSliceId))
                {
                    errMsg = "班段编号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(haltInfo.RegTypeId))
                {
                    errMsg = "号类别为空";
                    retVal = "1";
                }
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidatePayInfo(string payXml, out string errMsg, out PayInfo payInfo)
        {
            payInfo = new PayInfo();
            errMsg = "";
            var retVal = "0";
            try
            {
                payInfo = (PayInfo)XmlToObject(payXml, new PayInfo());
                if (string.IsNullOrEmpty(payInfo.PatientId))
                {
                    errMsg = "患者编号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(payInfo.ClinicNo))
                {
                    errMsg = "就诊流水号为空";
                    retVal = "1";
                    return retVal;
                }
                if (string.IsNullOrEmpty(payInfo.OrderDate))
                {
                    errMsg = "开单日期为空";
                    retVal = "1";
                    return retVal;
                }
                else
                {
                    var date = new DateTime();
                    if (!DateTime.TryParse(payInfo.OrderDate, out date))
                    {
                        errMsg = "开单日期格式不正确，请参考:yyyy-MM-dd";
                        retVal = "1";
                        return retVal;
                    }
                }
                if (string.IsNullOrEmpty(payInfo.SumFee))
                {
                    errMsg = "缴费单费用为空";
                    retVal = "1";
                    return retVal;
                }
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidateReportInfo(string reportXml, out string errMsg)
        {
            errMsg = "";
            var retVal = "0";
            try
            {
                var payInfo = (ReportInfo)XmlToObject(reportXml, new ReportInfo());
                if (string.IsNullOrEmpty(payInfo.PatientId))
                {
                    errMsg = "患者编号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(payInfo.ReportType))
                {
                    errMsg = "报告类型为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(payInfo.AppDate))
                {
                    errMsg = "申请日期为空";
                    retVal = "1";
                }
                else
                {
                    var date = new DateTime();
                    if (!DateTime.TryParse(payInfo.AppDate, out date))
                    {
                        errMsg = "申请日期格式不正确，请参考:yyyy-MM-dd";
                        retVal = "1";
                    }
                }
                if (string.IsNullOrEmpty(payInfo.AppNo))
                {
                    errMsg = "申请单号为空";
                    retVal = "1";
                }
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidateRefundInfo(string refundXml, out string errMsg)
        {
            errMsg = "";
            var retVal = "0";
            try
            {
                var payInfo = (RefundInfo)XmlToObject(refundXml, new RefundInfo());

                //Response4Notification response = new Response4Notification();
                //response.ResultCode = "0";
                //response.ResultDesc = "处理成功";
                //var responseInfo = new Response4NotificationSubResponseInfo();
                //responseInfo.RefundId = "123345";
                //responseInfo.RefundTime = "20151230120000";
                //response.ResponseInfo = responseInfo;

                //var responseXml = ToXml(response);

                if (string.IsNullOrEmpty(payInfo.OrderType))
                {
                    errMsg = "订单类型为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(payInfo.OrderId))
                {
                    errMsg = "订单号为空";
                    retVal = "1";
                }
                if (string.IsNullOrEmpty(payInfo.RefundType))
                {
                    errMsg = "退款类型为空";
                    retVal = "1";
                }

                if (string.IsNullOrEmpty(payInfo.RefundFee))
                {
                    errMsg = "退款金额为空";
                    retVal = "1";
                }

            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidateBillInfo(string billXml, out string errMsg, out BillInfo payInfo)
        {
            payInfo = new BillInfo();
            errMsg = "";
            var retVal = "0";
            try
            {
                payInfo = (BillInfo)XmlToObject(billXml, new BillInfo());
                
                if (string.IsNullOrEmpty(payInfo.ClinicNo))
                {
                    errMsg = "就诊流水号为空";
                    retVal = "1";
                }
               
                if (string.IsNullOrEmpty(payInfo.SumFee))
                {
                    errMsg = "缴费单金额为空";
                    retVal = "1";
                }
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ValidateOrderInfo(string billXml, out string errMsg, out string orderId)
        {
            errMsg = "";
            var retVal = "0";
            orderId = "";
            try
            {
                var payInfo = (OrderInfo)XmlToObject(billXml, new OrderInfo());
                if (string.IsNullOrEmpty(payInfo.OrderId))
                {
                    errMsg = "订单号为空";
                    retVal = "1";
                    return retVal;
                }
                orderId = payInfo.OrderId;
            }
            catch (Exception ex)
            {
                retVal = "1";
                errMsg = "发生异常:" + ex.Message;
            }
            return retVal;
        }

        public static string ToXml(Object oObject)
        {
            var xmlDoc = new XmlDocument();
            var xmlSerializer = new XmlSerializer(oObject.GetType());
            var nameSpace = new XmlSerializerNamespaces();
            nameSpace.Add("", ""); //清除命名空间
            using (var xmlStream = new MemoryStream())
            {
                var xmlWriter = XmlWriter.Create(xmlStream, new XmlWriterSettings { Encoding = Encoding.UTF8 }); //增加utf-8 编码方式
                xmlSerializer.Serialize(xmlWriter, oObject, nameSpace);

                xmlStream.Position = 0;
                xmlDoc.Load(xmlStream);

                return xmlDoc.InnerXml;
            }
        }

        public static Object XmlToObject(string xmlString, Object oObject)
        {
            var oXmlSerializer = new XmlSerializer(oObject.GetType());
            oObject = oXmlSerializer.Deserialize(new StringReader(xmlString));
            return oObject;
        }

    }
}
